package com.qp.basics.auth.service;

import com.qp.basics.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;
import org.springframework.stereotype.Service;

@Service
public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCodeServices {
    private static final String AUTH_CODE_KEY = "oauth_code";
    @Autowired
    private RedisUtils redisUtils;

    @Override
    protected OAuth2Authentication remove(String code) {
        OAuth2Authentication authentication = (OAuth2Authentication) redisUtils.hget(AUTH_CODE_KEY, code);
        if (authentication != null) {
            redisUtils.hdel(AUTH_CODE_KEY, code);
        }
        return authentication;
    }

    @Override
    protected void store(String code, OAuth2Authentication authentication) {
        redisUtils.hset(AUTH_CODE_KEY, code, authentication);
    }
}
